package jiandian;

/**
 * @program: javase
 * @description: 字符串压缩
 * @author: luolidong
 * @create: 2021-07-07 10:09
 * @version: 1.0
 */
public class Demo {
    //abbcc
    public String compressString(String S) {
        if (S.length() == 0) return "";
        int fast = 1;
        int slow = 0;
        StringBuilder sb = new StringBuilder();
        while (fast < S.length()) {
            if (S.charAt(fast) != S.charAt(slow)) {
                sb.append(S.charAt(slow));
                sb.append(fast - slow);
                slow=fast;
                fast++;

            } else {
                fast++;
            }
        }
        //添加最后
        sb.append(S.charAt(slow)).append(fast - slow);
        if (sb.length() >= S.length()) return S;
        return sb.toString();
    }

    public static void main(String[] args) {
        String abbbssaaa = new Demo().compressString("ab");
        System.out.println(abbbssaaa);
    }
}
